Skip to content

fix(util): tolerate deleted temporary environment variables - #2340

Merged
Byron merged 2 commits into
gitpython-developers:mainfrom
Abedyn:fix/patch-env-deleted-variable
Nov 5, 2026
Merged

Byron merged 2 commits into
gitpython-developers:mainfrom
Abedyn:fix/patch-env-deleted-variable

Conversation

@Abedyn

@Abedyn Abedyn commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

patch_env() raises KeyError on exit if its variable was originally absent and the context body deletes it. If the body also raises an exception, the cleanup error masks that exception.

Use os.environ.pop(name, None) when restoring an originally absent variable. Add 18 regression cases covering absent, empty and populated original values; unchanged, replaced and deleted temporary values; and normal and exceptional exits.

Motivation: this was discovered while inspecting GitPython with a user seeking their first small Python contribution to Git tooling on Windows. The goal is to make the environment helper's restoration behavior consistent when a caller changes the temporary variable. Codex prepared the implementation, tests and this description.

Validation on Windows, Python 3.13.17 and Git 2.56.0.windows.4:

  • Before the fix: 2 failed, 16 passed.
  • After the fix: 18 passed with GIT_PYTHON_BACKEND=cli and python -m pytest --noconftest -o addopts='' test/test_patch_env.py -q --tb=short.
  • Ruff lint and formatting checks passed for both changed files; git diff --check passed.

The full suite and its conftest hooks were not run. Installing the test extra with binary-only GixPython failed because no compatible Windows wheel for GixPython 0.3.0 was available. The focused tests do not use repository fixtures or Gix.

When a `patch_env()` body deletes a variable that was initially absent,
cleanup raises `KeyError` and can mask an exception from the body.
Use `os.environ.pop(name, None)` to restore absence idempotently while
preserving restoration of existing values.

Add 18 regression cases for absent, empty and populated original values,
keep/replace/delete body mutations and normal/exceptional exits.
The focused CLI-backend tests changed from 2 failed and 16 passed to
18 passed. Ruff lint/format and `git diff --check` passed. The full suite
was not run because its required `GixPython` Windows wheel was unavailable.

Agent-Model: unknown

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are reported for the change that prevents environment cleanup from masking context-body exceptions.

No open issues 🎉

What changed in this PR

This PR makes GitPython’s patch_env() cleanup tolerate deletion of a temporary environment variable, preventing a cleanup KeyError from masking an exception raised inside the context.

Changes:

  • Use os.environ.pop(name, None) when restoring an originally absent variable.
  • Add 18 regression cases covering original values, temporary-variable mutations, and normal or exceptional exits.

Review by GitHub Copilot.

File Description
test/​test_patch_env.py Tests environment restoration and preservation of context-body exceptions.
git/​util.py Makes cleanup safe when an originally absent variable has already been deleted.

Review effort: Balanced

@Byron
Byron merged commit f6db6e7 into gitpython-developers:main Nov 5, 2026
56 of 64 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants